package com.vq3.digitalizationsoftwareintegrates.Service.User.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Urole_PermissionEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;

import com.vq3.digitalizationsoftwareintegrates.Entity.User.User_RoleEntity;
import com.vq3.digitalizationsoftwareintegrates.Mapper.User.*;

import lombok.SneakyThrows;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    UserMapper userMapper;
    @Resource
    UserPermissionMapper userPermissionMapper;
    @Resource
    User_RoleMapper user_roleMapper;
    @Resource
    URole_TBMapper uRole_tbMapper;
    @Resource
    Urole_PermissionMapper urole_permissionMapper;
    @Resource
    Permission_TBMapper permission_tbMapper;


    @SneakyThrows
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException{
        ArrayList<GrantedAuthority> authorities = new ArrayList<>();
        QueryWrapper<UserEntity> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uname",userName);
        org.springframework.security.core.userdetails.User user = null;
        UserEntity users = userMapper.selectOne(userQueryWrapper);


        if (users != null) {
            if (users.getDeletestatus() == 1){

            Integer uid = users.getUid();
            QueryWrapper<User_RoleEntity> userRoleEntityQueryWrapper = new QueryWrapper<>();
            userRoleEntityQueryWrapper.eq("user_id",uid);
            List<User_RoleEntity> user_roleEntities = user_roleMapper.selectList(userRoleEntityQueryWrapper);

            List<Urole_PermissionEntity> urole_permissionEntities = new ArrayList<>();
            for (User_RoleEntity user_roleEntity : user_roleEntities) {
                QueryWrapper<Urole_PermissionEntity> urole_permissionMapperQueryWrapper = new QueryWrapper<>();
                Integer uroleId = user_roleEntity.getUroleId();
                urole_permissionMapperQueryWrapper.eq("u_p_urole_id",uroleId);
                List<Urole_PermissionEntity> urole_permissionEntities1 = urole_permissionMapper.selectList(urole_permissionMapperQueryWrapper);
                urole_permissionEntities.addAll(urole_permissionEntities1);
            }

            List<Permission_TBEntity> permission_tbEntities = new ArrayList<>();
            for (Urole_PermissionEntity urole_permissionEntity : urole_permissionEntities) {
                QueryWrapper<Permission_TBEntity> permission_tbEntityQueryWrapper = new QueryWrapper<>();
                Integer u_p_permission_id = urole_permissionEntity.getUPPermissionId();
                permission_tbEntityQueryWrapper.eq("permission_id",u_p_permission_id);
                List<Permission_TBEntity> permission_tbEntities1 = permission_tbMapper.selectList(permission_tbEntityQueryWrapper);
                permission_tbEntities.addAll(permission_tbEntities1);
            }
            for (Permission_TBEntity permission_tbEntity : permission_tbEntities) {
                authorities.add(new SimpleGrantedAuthority(permission_tbEntity.getPermission()));
            }

            user = new org.springframework.security.core.userdetails.User(users.getUname(),users.getUpassword(), authorities);
        }else {
                throw new DisabledException("当前账户状态异常！");
            }
        }else {
            throw new InternalAuthenticationServiceException("账户可能不存在！");
        }
        return user;

    }

}
